package cn.iocoder.yudao.module.jikong.service.jkOrder;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONObject;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictDataDO;
import cn.iocoder.yudao.module.system.dal.mysql.dict.DictDataMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.jikong.controller.admin.jkOrder.vo.*;
import cn.iocoder.yudao.module.jikong.dal.dataobject.jkOrder.JkOrderDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.jikong.dal.mysql.jkOrder.JkOrderMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.jikong.enums.ErrorCodeConstants.*;

/**
 * 单据 Service 实现类
 *
 * @author xc
 */
@Service
@Validated
@Slf4j
public class JkOrderServiceImpl implements JkOrderService {

    @Resource
    private JkOrderMapper jkOrderMapper;
    @Resource
    private DictDataMapper dictDataMapper;

    @Override
    public Long createJkOrder(JkOrderSaveReqVO createReqVO) {
        // 插入
        JkOrderDO jkOrder = BeanUtils.toBean(createReqVO, JkOrderDO.class);
        jkOrderMapper.insert(jkOrder);
        // 返回
        return jkOrder.getId();
    }

    @Override
    public void updateJkOrder(JkOrderSaveReqVO updateReqVO) {
        // 校验存在
        validateJkOrderExists(updateReqVO.getId());
        // 更新
        JkOrderDO updateObj = BeanUtils.toBean(updateReqVO, JkOrderDO.class);
        jkOrderMapper.updateById(updateObj);
    }



    @Override
    public void deleteJkOrder(Long id) {
        // 校验存在
        validateJkOrderExists(id);
        // 删除
        jkOrderMapper.deleteById(id);
    }

    @Override
        public void deleteJkOrderListByIds(List<Long> ids) {
        // 校验存在
        validateJkOrderExists(ids);
        // 删除
        jkOrderMapper.deleteByIds(ids);
        }

    private void validateJkOrderExists(List<Long> ids) {
        List<JkOrderDO> list = jkOrderMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(JK_ORDER_NOT_EXISTS);
        }
    }

    private void validateJkOrderExists(Long id) {
        if (jkOrderMapper.selectById(id) == null) {
            throw exception(JK_ORDER_NOT_EXISTS);
        }
    }

    @Override
    public JkOrderDO getJkOrder(Long id) {
        return jkOrderMapper.selectById(id);
    }

    @Override
    public PageResult<JkOrderDO> getJkOrderPage(JkOrderPageReqVO pageReqVO) {
        return jkOrderMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<JkOrderDO> getJkOrderPageExpert(JkOrderPageReqVO pageReqVO) {
        return jkOrderMapper.selectPageExpert(pageReqVO);
    }


    @Override
    public List<JSONObject> statusCount() {
        // 查询所有类型的数量，根据类型分组查询用sql就行
        List<Map<String, Long>> statusCounts = jkOrderMapper.selectStatusCountByStatus();

        // 将结果转换为Map，以便快速查找
        Map<String, Long> statusCountMap = new HashMap<>();
        for (Map<String, Long> item : statusCounts) {
            statusCountMap.put(String.valueOf(item.get("status")), item.get("count"));
        }

        // 查询字典数据，获取订单状态信息
        List<DictDataDO> jkOrderStatusDict = dictDataMapper.selectList(
                new LambdaQueryWrapperX<DictDataDO>()
                        .eq(DictDataDO::getStatus, 0)
                        .eq(DictDataDO::getDictType, "jk_order_status")
        );

        // 构造返回结果
        List<JSONObject> result = new ArrayList<>();
        for (DictDataDO dictDataDO : jkOrderStatusDict) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.set("name", dictDataDO.getLabel());
            jsonObject.set("status", dictDataDO.getValue());

            // 根据status查找对应的count，如果不存在则设置为0
            Long count = statusCountMap.getOrDefault(dictDataDO.getValue(), 0L);
            jsonObject.set("count", count);

            result.add(jsonObject);
        }

        return result;
    }


}
